﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

namespace WebApplication1
{
    public partial class AddCater : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Bind site to drop down
            if (!IsPostBack)
            {
                SiteManagement sitemgm = new SiteManagement();
                List<Site> sites = sitemgm.GetAllSite();

                foreach (Site siteobj in sites)
                {
                    ListItem li = new ListItem();
                    li.Text = siteobj.name;
                    li.Value = siteobj.id.ToString();
                    dropdownSite.Items.Add(li);
                }
                listviewCaterer.DataBind();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            fos_User newUser = new fos_User();
            UserManagement usermgt = new UserManagement();

            newUser.Name = txtName.Text;
            newUser.WWID = "Caterer";
            newUser.Phone = txtPhone.Text;
            newUser.Password = Security.EncodePassword(txtPassword.Text);
            newUser.Email = txtEmail.Text;


            string roleid = "3"; // this is caterer role id
            string siteid = dropdownSite.SelectedItem.Value.ToString();
            string printerid = dropdownPrinter.SelectedValue.ToString();

            //usermgt.GetUsersByRole("3");

            newUser.IsDeleted = 1;
            try
            {
                usermgt.SaveUser(newUser, Security.EncodePassword(txtRetypePassword.Text), true);
                usermgt.AssociateSite(newUser, siteid);
                usermgt.AssociateCatererSite(newUser, siteid);
                usermgt.AssociateRole(newUser, roleid);
                usermgt.AssociatePrinter(newUser, printerid);
                usermgt.SendActivationLink(newUser);
                divMessage.Visible = true;
                divMessage.CssClass = "alert alert-success  alert-dismissible";
                lblMessage.Text = "Thank you for registering at our site. Please check your email account for verification link!";

            }
            catch (Exception ex)
            {
                divMessage.Visible = true;
                divMessage.CssClass = "alert alert-danger  alert-dismissible";
                lblMessage.Text = ex.Message;
            }

            listviewCaterer.DataBind();
        }

        protected void odsCaterer_Updating(object sender, ObjectDataSourceMethodEventArgs e)
        {
            if (e.InputParameters.Count != 0)
            {
                object idval = e.InputParameters["Id"];
                object phoneval = e.InputParameters["Phone"];
                object emailval = e.InputParameters["Email"];
                e.InputParameters["lastModifiedBy"] = "System";
                e.InputParameters["roleid"] = "3";
                // Get from dropdownlist
                DropDownList ddlSiteType;
                ddlSiteType = (DropDownList)listviewCaterer.EditItem.FindControl("DropDownList1");
                e.InputParameters["siteid"] = ddlSiteType.SelectedValue.ToString();

                DropDownList ddlPrinterType;
                ddlPrinterType = (DropDownList)listviewCaterer.EditItem.FindControl("DropDownList2");
                e.InputParameters["printerid"] = ddlPrinterType.SelectedValue.ToString();
            }


        }

        protected void odsCaterer_DataBinding(object sender, EventArgs e)
        {
            int i = 1;
        }

        protected void listviewCaterer_DataBinding(object sender, EventArgs e)
        {
            int i = 1;

        }
        public List<Site> GetAllSite1()
        {
            //Bind site to drop down
            SiteManagement sitemgm = new SiteManagement();
            List<Site> sites = sitemgm.GetAllSite();
            return sites;
            foreach (Site siteobj in sites)
            {
                ListItem li = new ListItem();
                li.Text = siteobj.name;
                li.Value = siteobj.id.ToString();
                dropdownSite.Items.Add(li);
            }

        }

        protected void DropDownList1_CallingDataMethods(object sender, CallingDataMethodsEventArgs e)
        {

        }

        public String CheckSite(object datalist)
        {
            string retval = "";
            List<fos_UserLinkSite> updateSiteList = new List<fos_UserLinkSite>();
            foreach (fos_UserLinkSite linksite in (System.Collections.Generic.HashSet<fos_UserLinkSite>)datalist)
            {
                if (linksite.IsDeleted != 1)
                {
                    updateSiteList.Add(linksite);
                    retval = linksite.fos_Site.SiteName;
                }
            }
            return retval;
        }

        public List<fos_SiteLinkPrinter> RemoveIsDeletedPrinter(object datalist)
        {
            List<fos_SiteLinkPrinter> updatePrinterList = new List<fos_SiteLinkPrinter>();
            foreach (fos_SiteLinkPrinter linkprinter in (System.Collections.Generic.HashSet<fos_SiteLinkPrinter>)datalist)
            {
                if (linkprinter.IsDeleted != 1)
                {
                    updatePrinterList.Add(linkprinter);
                }
            }
            return updatePrinterList;
        }

        public String CheckPrinter(object datalist)
        {
            String retval = "";
            List<fos_SiteLinkPrinter> updatePrinterList = new List<fos_SiteLinkPrinter>();
            foreach (fos_SiteLinkPrinter linkprinter in (System.Collections.Generic.HashSet<fos_SiteLinkPrinter>)datalist)
            {
                if (linkprinter.IsDeleted != 1)
                {
                    updatePrinterList.Add(linkprinter);
                    retval = (linkprinter.fos_Printer.PrinterName).ToString();
                }
            }
            return retval;
        }

        protected void odsCaterer_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
        {
        }

        protected void odsCaterer_Deleted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            if (e.Exception == null)
            {
                divMessage.Visible = true;
                divMessage.CssClass = "alert alert-success  alert-dismissible";
                lblMessage.Text = "Cafe deleted successfully";
            }
            else
            {

                divMessage.Visible = true;
                divMessage.CssClass = "alert alert-danger  alert-dismissible";
                lblMessage.Text = (e.Exception).InnerException.Message.ToString();
                e.ExceptionHandled = true;
            }
        }

        protected void odsCaterer_Updated(object sender, ObjectDataSourceStatusEventArgs e)
        {
            if (e.Exception == null)
            {
                divMessage.Visible = true;
                divMessage.CssClass = "alert alert-success  alert-dismissible";
                lblMessage.Text = "Cafe updated successfully";
            }
            else
            {

                divMessage.Visible = true;
                divMessage.CssClass = "alert alert-danger  alert-dismissible";
                lblMessage.Text = (e.Exception).InnerException.Message.ToString();
                e.ExceptionHandled = true;
            }
        }

        protected void listviewCaterer_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            //Verify there is an item being edited.
            if (listviewCaterer.EditIndex >= 0)
            {

                //Get the item object.
                ListViewDataItem dataItem = (ListViewDataItem)e.Item;

                // Check for an item in edit mode.
                if (dataItem.DisplayIndex == listviewCaterer.EditIndex)
                {
                    // Printer
                    DropDownList list2 = (DropDownList)dataItem.FindControl("DropDownList2");

                    Label oriPrinter = (Label)dataItem.FindControl("lblExPrinter");
                    ListItem item2 = list2.Items.FindByText(oriPrinter.Text);
                    list2.SelectedIndex = list2.Items.IndexOf(item2);


                    // Site
                    DropDownList list = (DropDownList)dataItem.FindControl("DropDownList1");

                    Label oriSite = (Label)dataItem.FindControl("lblSite");
                    ListItem item = list.Items.FindByText(oriSite.Text);
                    list.SelectedIndex = list.Items.IndexOf(item);


                }
            }
        }
    }
}